package com.pb.metadata.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.entity.common.DictVO;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.util.StringUtil;
import com.pb.infra.oth.util.TreeUtil;
import com.pb.infra.oth.util.UserInfo;
import com.pb.metadata.mapper.CoefficientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系数管理
 * @author lzh
 * @date 2023/03/21
 */
@Service
public class CoefficientService {

    /**
     * 导入配置多表DAO映射
     */
    @Resource
    private CoefficientMapper coefficientMapper;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询系数树
     *
     * @return
     */
    public Result selectPara(Map<String, Object> map){
        return Result.ok(coefficientMapper.selParaTree(map));
    }

    /**
     * 保存系数
     * @param paramMap 保存数据
     * @return
     */
    @Transactional
    public Result insert(Map<String, Object> paramMap, HttpServletRequest request){

        //获取请求体内的token
        String token = request.getHeader(jwtTokenConfig.getHeader());
        //通过token在redis中获取用户信息
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(token);
//        Integer maxId = coefficientMapper.selMaxId();

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateNowStr = sdf.format(d);

        paramMap.put("paraId", paramMap.get("paraCodeId").toString());
        paramMap.put("createUser", userInfo.getUserId());
        paramMap.put("createDate", dateNowStr);
        coefficientMapper.addParaTree(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     *修改系数
     * @param paramMap 保存数据
     * @return
     */
    @Transactional
    public Result update(Map<String, Object> paramMap){
        coefficientMapper.editParaTree(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 删除系数
     *
     * @param paramMap 请求参数
     */
    @Transactional
    public Result delete(Map<String, Object> paramMap) {
        List paraIds = (List) paramMap.get("paraIds");
        String corpId = paramMap.get("corpId").toString();
        coefficientMapper.delParaTree(corpId, paraIds);
        coefficientMapper.delParaAllVal(corpId, paraIds);
        return Result.ok("删除成功");
    }

    /**
     * 校验系数名字是否存在
     * @return
     */
    public Result validUniqueName(Map<String, Object> paramMap) {
        List<Map<String, Object>> list = coefficientMapper.validUniqueName(paramMap);
        if (!StringUtil.isEmpty(list) && list.size() > 0) {
            return Result.ok(false);
        } else {
            return Result.ok(true);
        }
    }

    /**
     * 查询系数值
     * @param map
     * @param page
     * @return
     */
    public Result selectParaVal(Map<String, Object> map, Page page){
        if (page != null && page.getPageIndex() != null && page.getPageSize() !=null){
            PageHelper.startPage(page.getPageIndex(),page.getPageSize());
        }
        List<Map<String, Object>> list = coefficientMapper.selParaVal(map);
        return Result.ok(new PageInfo(list).getTotal(),list);
    }

    /**
     *保存系数值
     * @param paramMap 保存数据
     * @return
     */
    @Transactional
    public Result insertParaVal(Map<String, Object> paramMap, HttpServletRequest request){
        List<Map<String, Object>> list = coefficientMapper.validParaValue(paramMap);
        //获取请求体内的token
        String token = request.getHeader(jwtTokenConfig.getHeader());
        //通过token在redis中获取用户信息
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(token);

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateNowStr = sdf.format(d);
        paramMap.put("id", coefficientMapper.selId());
        if (list.size() > 0) {
            return Result.error("100", "该参数下面已存在同法人同机构同部门同科目同产品及同生效日期的数据");
        } else {
            paramMap.put("createUser", userInfo.getUserId());
            paramMap.put("createDate", dateNowStr);
            coefficientMapper.addParaVal(paramMap);
            return Result.OK_EMPTY;
        }
    }

    /**
     *修改系数值
     * @param paramMap 保存数据
     * @return
     */
    @Transactional
    public Result updateParaVal(Map<String, Object> paramMap){
        List<Map<String, Object>> list = coefficientMapper.validParaValue(paramMap);

        if (list.size() > 0) {
            return Result.error("100", "该参数下面已存在同法人同机构同部门同科目同产品及同生效日期的数据");
        } else {
            coefficientMapper.editParaVal(paramMap);
            return Result.ok("修改成功");
        }
    }

    /**
     * 删除系数值
     *
     * @param paramMap 请求参数
     */
    @Transactional
    public Result deleteParaVal(Map<String, Object> paramMap) {
        List<Integer> list = (List<Integer>)paramMap.get("ids");
        coefficientMapper.delParaVal(list);
        return Result.ok("删除成功");
    }


    /**
     * 查询科目树
     * @param paramMap
     * @return
     */
    public Result getSubjTree(Map<String, Object> paramMap) {
        List<Map<String, Object>> subjTree = coefficientMapper.getSubjTree(paramMap);
        return Result.ok(subjTree);
    }

    /**
     * 查询产品树
     * @param paramMap
     * @return
     */
    public Result getPrdtTree(Map<String, Object> paramMap) {
        List<Map<String, Object>> list = coefficientMapper.getPrdtTree(paramMap);
        return Result.ok(list);
    }

    public Result getDimTree(Map<String, String> paramMap) {
        List<DictVO> list = coefficientMapper.getDimTree(paramMap);
        Object root=null;
        if (list.size()>0){
            root =list.get(0).parentId;
        }

        return Result.ok(TreeUtil.buildByRecursiveRootEmpty(list, root));
    }
}
